package com.tongfusoft.deal.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tongfusoft.contract.service.ContractService;
import com.tongfusoft.da.help.DBUtils;
import com.tongfusoft.deal.common.VariableStore;
import com.tongfusoft.deal.common.VariableStore.ContractConfig;
import com.tongfusoft.utils.UIDGeneratorService;


public class DealAction extends BaseWarpAction{
	
	private ContractService contractService = new ContractService();
	
	
//	protected HttpServletRequest request = getRequest();
	/**
	 * 摘牌
	 * @return
	 */
	public String delist () {
		
		String orderType = getParameter("ordertype");	//摘牌类型
		String price = getParameter("price");			//单价
		String lsinfoId = getParameter("lsinfoId");	//信息编号
		String memberId = getParameter("memberId");	//会员编号
		
		getRequest().setAttribute("price", price);
		getRequest().setAttribute("treaty", orderType);
		getRequest().setAttribute("lsinfoId", lsinfoId);
		getRequest().setAttribute("memberId", memberId);
		
		
//		Map<String,Object>contractTemp = contractService.get(orderType);
//		showMap(contractTemp);
		
		return "delist";
	}
	
	/**
	 * 填写清单
	 * @return
	 */
	public String listing () {
		
		String formtype = getParameter("formtype");
		System.out.println(formtype);
		getRequest().setAttribute("formtype", formtype);
		
		return "listing";
	}
	
	/**
	 * 提交订单
	 * @return
	 */
	public String listed () {
		
		String ordertype = getParameter("ordertype");
		String uuid = getParameter("id");
		
//		Map<String,Object> param = getParametersStartingWith("insert_");
		
		List<Map<String, Object>> list = null;
		
		HashMap<String,Object> hmap = new HashMap<String,Object>();
		
		if(VariableStore.DealType.LOGISTICS_SUPPLY.equals(ordertype)) {
			
			hmap.put("lsorderId", uuid);
			list = DBUtils.executeSelect("/deal#TLT_LS_ORDER", hmap);
			
		}else if (VariableStore.DealType.LOGISTICS_NEEDS.equals(ordertype)) {
			
			hmap.put("ldorderId", uuid);
			list = DBUtils.executeSelect("/deal#TLT_LD_ORDER", hmap);
			
		}else if (VariableStore.DealType.STORAGE_SUPPLY.equals(ordertype)) {
			
			hmap.put("ssorderId", uuid);
			list = DBUtils.executeSelect("/deal#TLT_SS_ORDER", hmap);
			
		}else if (VariableStore.DealType.STORAGE_NEEDS.equals(ordertype)) {
			
			hmap.put("sdorderId", uuid);
			list = DBUtils.executeSelect("/deal#TLT_SD_ORDER", hmap);
			
		}
		
		Map<String,Object>contractTemp = contractService.get(ordertype);
		
		String content = clobToString(contractTemp.get("TEMPLATE_CONTENT"));
		
		Map<String,Object> result =  list.get(0);
		
		content = content.replaceAll(ContractConfig.PARTYA, objToString(result.get("MEMBER_ID")))
				.replaceAll(ContractConfig.PARTYB, getCurrUser().getUserDisplayName())//objToString(result.get("TOTAL_AMOUNT"))
				.replaceAll(ContractConfig.MONEY, objToString(result.get("TOTAL_AMOUNT")));
	
		result.put("partyB", getCurrUser().getUserDisplayName());
		getRequest().setAttribute("content", content);
		getRequest().setAttribute("lsorderId", uuid);
		getRequest().setAttribute("ordertype", ordertype);
		getRequest().setAttribute("templateId", contractTemp.get("TEMPLATE_ID"));
		getRequest().setAttribute("orderForm", result);
		
		getRequest().setAttribute("code", UIDGeneratorService.getUUID());
		getRequest().setAttribute("name", UIDGeneratorService.getUUID());
		
		return "listed";
	}
	
	
	/**
	 * 签合同
	 * @return
	 */
	public String singContract () {
		
		
		Map<String,Object> param = getParametersStartingWith("insert_");
		
		param.put("contractId", UIDGeneratorService.getUUID());
		DBUtils.executeInsert("/deal#TCM_CONTRACT_TRADE", param);
		
		
		return "singContract";
	}

	
	
}
